clear
set more off

/* Inputs */ 

local in_varsel "/home/projects/cedsei/data/A1_varseldata.dta"	 // Produced by Jonas C. 

/* Outputs */ 

local out_ind        "/data2/cedsei/firm/not_ind_pop.dta"
local out_firms      "/data2/cedsei/firm/not_firm_pop.dta"

********************************************************************************

clear
use firmid persid inkom_dat list_dat varselid ankomst_dat anstupp_dat varselorsak using ///
`in_varsel'	

rename firmid lopnr_peorgnr 
rename persid lopnr
keep if !mi(lopnr_peorgnr) & !mi(lopnr)

egen temp = mode(inkom_dat), by(varselid lopnr_peorgnr)
format temp %td	
replace list_dat = temp if mi(list_dat)

sort lopnr_peorgnr lopnr list_dat
keep lopnr_peorgnr list_dat lopnr anstupp_dat varselorsak varselid
drop if mi(list_dat)

gen list_dat_ym    = ym(year(list_dat),month(list_dat)) 
gen anstupp_dat_ym = ym(year(anstupp_dat),month(anstupp_dat))
format list_dat_ym anstupp_dat_ym %tm 
gen de_facto = anstupp_dat_ym - list_dat_ym + 1
tab de_facto
gen temp = !inrange(de_facto,1,18)
count if de_facto<1 
count 
count if de_facto>18
egen inconsistent = mean(temp), by(lopnr_peorgnr)
sum inconsistent, d
count
drop if inconsistent>0.1
drop if temp == 1 
drop temp inconsistent
count 
duplicates drop 

order lopnr_peorgnr lopnr list_dat anstupp_dat list_dat_ym anstupp_dat_ym 
keep lopnr_peorgnr lopnr list_dat anstupp_dat list_dat_ym anstupp_dat_ym varselid
gen year = year(list_dat)

keep if inrange(year,2005,2018)

// The below investigates the number of times a person has been notified from 
// the same employer. Remove firms that have 10% or more notified individuals 
// who are notified multiple times from the same employer. 
egen m1 = min(list_dat_ym), by(lopnr_peorgnr lopnr)
egen m2 = max(list_dat_ym), by(lopnr_peorgnr lopnr)
format m1 m2 %tm
gen temp = m1 == m2
egen mtem = mean(temp), by(lopnr_peorgnr)
sum mtem, d
count
drop if mtem<.1
count

drop m1 m2 temp mtem 

gcollapse (min) list_dat list_dat_ym (max) anstupp_dat anstupp_dat_ym, by(lopnr lopnr_peorgnr year)

duplicates drop 
isid lopnr_peorgnr lopnr year  
save "`out_ind'", replace

keep lopnr_peorgnr 
duplicates drop 
save "`out_firms'", replace



